/*
 * Copyright (C) 2020 UNISOC Open Source Project
 *
 */

package com.unisoc.systemui.screenshot;

import android.app.KeyguardManager;
import android.content.Context;
import android.content.Intent;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.Settings;
import android.util.Log;
import com.android.internal.util.LongScreenshotUtil;

import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
import static com.unisoc.floatkey.SystemUIFloatKey.ACTION_FLOATKEY_HIDE;
import static com.unisoc.floatkey.SystemUIFloatKey.ACTION_FLOATKEY_RESTART;

public class CommonUtils {
    private static final String TAG = "CommonUtils";

    /**
     * Current is in region select or longscreenshot mode.
     *
     * @param context
     * @return true if in region select or longscreenshot mode.
     */
    public static boolean isInRegionOrLongshotMode(Context context) {
        int result = Settings.System.getIntForUser(context.getContentResolver(),
                LongScreenshotUtil.LONGSCREENSHOT_SETTING, 0, UserHandle.USER_OWNER);
        Log.d(TAG, "get longshot_switch result:" + result);
        return result == 1;
    }

    /**
     * Set current mode for region select or longscreenshot mode.
     *
     * @param context
     * @param value    0 or 1, 0 is not in mode and 1 is in mode.
     */
    public static void setRegionOrLongshotMode(Context context, int value) {
        Log.d(TAG, "setRegionOrLongshotMode:" + value);
        Settings.System.putIntForUser(context.getContentResolver(),
                LongScreenshotUtil.LONGSCREENSHOT_SETTING, value, UserHandle.USER_OWNER);
    }

    /**
     * get assistant value.
     *
     * @param context.
     * @param value    0 or 1, 0 is assistant is disable and 1 is enable.
     */
    private static boolean getAssistantValue(Context context) {
        if (Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.ASSISTANT_ON, 0) == 1) {
            return true;
        }
        return false;
    }

    /**
     * disable assistant touch if current is enable.
     *
     * @param context.
     */
    public static void disableAssistantIfNeeded(Context context) {
        if (!getAssistantValue(context)) {
            return;
        }
        context.sendBroadcastAsUser(new Intent(ACTION_FLOATKEY_HIDE), UserHandle.ALL);
        Log.d(TAG, "disableAssistant");
    }

    /**
     * enable assistant touch if current is disable.
     *
     * @param context.
     */
    public static void enableAssistantIfNeeded(Context context) {
        if (!getAssistantValue(context)) {
            return;
        }
        /* UNISOC: Modify for bug1405745 @{ */
        KeyguardManager mKeyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
        UserManager mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);

        if (mKeyguardManager != null && !mKeyguardManager.isKeyguardLocked()
                && mUserManager != null && mUserManager.isUserUnlocked()) {
            context.sendBroadcastAsUser(new Intent(ACTION_FLOATKEY_RESTART), UserHandle.ALL);
            Log.d(TAG, "enableAssistant");
        }
        /* @} */
    }

    /* UNISOC: Modify for bug1406916 @{ */
    public static boolean isOrientationPortrait(Context context) {
        int orientation = context.getResources().getConfiguration().orientation;
        return orientation == ORIENTATION_PORTRAIT;
    }
    /* @} */
}
